//
//  MapBottomView.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/17.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "MapBottomView.h"

@interface MapBottomView ()

@property (nonatomic, strong) UIImageView *portraitView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UILabel *distanceLabel; //距离
@property (nonatomic, strong) BaseButton *btnNav; //导航按钮

@end

@implementation MapBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 20;
        self.layer.masksToBounds = YES;
        [self initUIMapBottomView];
    }
    return self;
}

- (void)initUIMapBottomView {
    // 添加图片，即商户图
    self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ADAPT_W(131), ADAPT_W(131))];
    self.portraitView.contentMode = UIViewContentModeScaleAspectFill;
    self.portraitView.clipsToBounds = YES;
    [self addSubview:self.portraitView];
    
    // 添加标题，即商户名
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ADAPT_W(10) + ADAPT_W(131), 15, self.frame.size.width - ADAPT_W(10) - ADAPT_W(131), 25)];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.text = @"";
    [self addSubview:self.titleLabel];
    
    // 添加副标题，即商户地址
    self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ADAPT_W(10) + ADAPT_W(131), GetViewYH(self.titleLabel) + 5, GetViewWidth(self.titleLabel), 33)];
    self.subtitleLabel.numberOfLines = 0;
    self.subtitleLabel.font = [UIFont systemFontOfSize:12];
    self.subtitleLabel.textColor = [UIColor lightGrayColor];
    self.subtitleLabel.text = @"";
    [self addSubview:self.subtitleLabel];

    {
        NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@" 导航"];
        [attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, attributedString1.length)];
        NSTextAttachment *attach1 = [[NSTextAttachment alloc] init];
        attach1.image = ImageNamed(@"路径");
        attach1.bounds = CGRectMake(0, -4, 12, 16);
        NSAttributedString *attachString1 = [NSAttributedString attributedStringWithAttachment:attach1];
        //将图片插入到合适的位置
        [attributedString1 insertAttributedString:attachString1 atIndex:0];
        
        self.btnNav = [[BaseButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 64 - 13, self.frame.size.height - 15 - 24, 64, 24)];
        [self.btnNav setAttributedTitle:attributedString1 forState:UIControlStateNormal];
        [self.btnNav addTarget:self action:@selector(actionMapNav) forControlEvents:UIControlEventTouchUpInside];
        [self.btnNav gradientViewColorArray:@[] pointArr:@[]];
        self.btnNav.titleLabel.font = FONT(12);
        [self addSubview:self.btnNav];
    }
    {
        self.distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(ADAPT_W(10) + ADAPT_W(131), self.frame.size.height - 19 - 17, GetViewX(self.btnNav) - ADAPT_W(10) - ADAPT_W(131), 17)];
        self.distanceLabel.font = FONT(12);
        self.distanceLabel.textColor = UIColorFromRGB(0x3170FF);
        [self addSubview:self.distanceLabel];
    }
}

- (void)actionMapNav {
    if (self.block) {
        self.block();
    }
}

- (void)setImageURL:(NSString *)imageURL {
    [self.portraitView sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:LHYPlaceholderImage];
}

- (void)setTitle:(NSString *)title {
    
    self.titleLabel.text = title;
}

- (void)setSubtitle:(NSString *)subtitle {
    self.subtitleLabel.text = subtitle;
}

- (void)setDistance:(CGFloat)distance {
    
    NSString *str = [NSString stringWithFormat:@" 距离您:%.1f km", distance / 1000];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = ImageNamed(@"位置");
    attach.bounds = CGRectMake(0, -4, 12, 16);
    NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
    //将图片插入到合适的位置
    [attributedString insertAttributedString:attachString atIndex:0];
    self.distanceLabel.attributedText = attributedString;
}

@end
